package com.chinacoal.microservice.service.impl;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.mapper.CcmsSmartleasingMessageMapper;
import com.chinacoal.microservice.pojo.CcmsSmartleasingDeviceManagement;
import com.chinacoal.microservice.pojo.CcmsSmartleasingMessage;
import com.chinacoal.microservice.pojo.Currency;
import com.chinacoal.microservice.service.MessageManagementService;
import com.chinacoal.microservice.util.QueryCriteria;
import com.chinacoal.microservice.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhuliang
 * @version V1.0
 * @Description: 运维管理-消息管理-后端实现类
 * @date 2020-12-31 16：43
 */
@Service
public class MessageManagementServiceImpl implements MessageManagementService {

    @Autowired
    private CcmsSmartleasingMessageMapper messageMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Description: 查询所有的消息信息（带分页）
     * @author zhuliang
     * @date 2020/12/31 16:50
     */
    @Override
    public Result<List<CcmsSmartleasingMessage>> getAllByPage(QueryCriteria queryCriteria, String token) {
        //得到登录人信息
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //设置查询条件
        Page<CcmsSmartleasingMessage> toPage = new Page<>(queryCriteria.getPage(), queryCriteria.getPageSize());

        //查询所有的消息信息
        List<CcmsSmartleasingMessage> messages = messageMapper.getAllByPage(toPage,
                                                                            currency.getProperty(),
                                                                            currency.getManagementOffice());


        return Result.success(messages, toPage.getTotal());
    }

    /**
     * @Description: 条件查询所有的消息信息（带分页）
     * @author zhuliang
     * @date 2020/12/31 16:51
     */
    @Override
    public Result<List<CcmsSmartleasingMessage>> getAllByCondition(QueryCriteria queryCriteria, String token) {
        //得到登录人信息
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //设置查询条件
        Page<CcmsSmartleasingMessage> toPage = new Page<>(queryCriteria.getPage(), queryCriteria.getPageSize());

        //条件查询所有的消息信息
        List<CcmsSmartleasingMessage> messages = messageMapper.getAllByCondition(toPage,
                                                                                queryCriteria.getEquipmentSerialNumber(),
                                                                                queryCriteria.getRegion(),
                                                                                queryCriteria.getFkEquipmentTypeId(),
                                                                                queryCriteria.getEquipmentName(),
                                                                                queryCriteria.getFkBuildingId(),
                                                                                queryCriteria.getFirstDate(),
                                                                                queryCriteria.getEndDate(),
                                                                                currency.getProperty(),
                                                                                currency.getManagementOffice());

        return Result.success(messages, toPage.getTotal());
    }
}
